<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-12-18
 * Time: 16:45
 */

namespace app\admin\controller;


use app\constant\ComplainConst;
use app\constant\SuggestConst;
use app\constant\WithdrawalsConst;
use app\model\AdminActives;
use app\model\AdminOrder;
use app\model\AppComment;
use app\model\AppComplain;
use app\model\AppSuggest;
use app\model\AppWithdrawals;
use app\util\ReturnCode;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class Statistics extends Base
{
    //活动效果数据
    public function getActiveData()
    {
        $activeId = $this->request->get('active_id');

        $where = [];
        if (isset($activeId)) {
            $where['goods_id'] = [$activeId];
        }
        $where = $this->addTid($where);
        try {
            $data = [];
            $result = (new AdminOrder())->where($where)->field('count(id) as count,trade_status,pay_status,sum(total_amount) as total_amount,sum(order_amount) as order_amount')->group(['trade_status', 'pay_status'])->select();
            $data['counts'] = $result;
            $dataByDate = (new AdminOrder())->where($where)->field("count(id) as count,sum(total_amount) as total_amount,sum(order_amount) as order_amount,date_format(create_time,'%Y-%m-%d') as date")->group("date")->select();
            $data['bydate'] = $dataByDate;
            return $this->buildSuccess($data);
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
        }
        return $this->buildFailed(ReturnCode::FAILED, '获取失败');
    }

    //待处理数据
    public function getNoticeData()
    {
        $data = [];
        //待处理提现数据
        $where = $this->addTid(['status' => WithdrawalsConst::STATUS_UN_OP]);
        $count = (new AppWithdrawals())->where($where)->count();
        //明天结束的活动
        $orderWhere = $this->addTid([]);
        $activeData = (new AdminActives())->where($orderWhere)->whereTime('end_time', '<=', date('Y-m-d H:i:s', strtotime('+1 day')))->count();
        //待处理建议数据
        $where = $this->addTid(['status' => SuggestConst::STATUS_UN_SOLVE]);
        $suggestCount = (new AppSuggest())->where($where)->count();
        //待处理投诉数据
        $where = $this->addTid(['status' => ComplainConst::STATUS_UN_SOLVE]);
        $complainCount = (new AppComplain())->where($where)->count();
        //今日新增评论
        $where = $this->addTid([]);
        $commentCount = (new AppComment())->where($where)->whereTime('create_time','today')->count();
        $data['withdrawals_count'] = $count;
        $data['active_about_end'] = $activeData;
        $data['suggest_count'] = $suggestCount;
        $data['complain_count'] = $complainCount;
        $data['comment_count'] = $commentCount;
        return $this->buildSuccess($data);
    }

}